//
//  LRCommentCell.swift
//  LRWeibo
//
//  Created by 雷路荣 on 15/12/26.
//  Copyright © 2015年 leilurong. All rights reserved.
//

import UIKit

class LRCommentCell: UITableViewCell {
    
    var commentsModel: LRCommentModel? {
        didSet{
            //设置头像
            if let urlString = commentsModel?.user?.profile_image_url {
                iconView.sd_setImageWithURL(NSURL(string: urlString), placeholderImage:UIImage(named: "avatar"))
            }
            //名称
            nameLabel.text = commentsModel?.user?.screen_name
            //会员等级图标
            mbrankImageView.image = commentsModel?.user?.mbrankImage
            //时间
            //timeLabel.text = commentsModel?.created_at ?? "未知时间"
            timeLabel.text = NSDate.sinaDateToDate(commentsModel?.created_at ?? "" ).sinaDateDescription()
            //来源
            sourceLabel.text = commentsModel?.source
            //认证图标
            verifiedImageView.image = commentsModel?.user?.verifiedImage
            //内容
            contentLabel.text = commentsModel?.text
        }
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        prepareUI()
    }
    //添加子控件
    func prepareUI() {
        //1 添加子控件
        contentView.addSubview(iconView)
        contentView.addSubview(nameLabel)
        contentView.addSubview(mbrankImageView)
        contentView.addSubview(timeLabel)
        contentView.addSubview(sourceLabel)
        contentView.addSubview(sourceLabel)
        contentView.addSubview(verifiedImageView)
        contentView.addSubview(contentLabel)
        //2 设置约束
        iconView.ff_AlignInner(type: ff_AlignType.TopLeft, referView: contentView, size: CGSizeMake(35, 35), offset: CGPointMake(LRStatusCellMargin, LRStatusCellMargin))
        nameLabel.ff_AlignHorizontal(type: ff_AlignType.TopRight, referView: iconView, size: nil, offset: CGPointMake(LRStatusCellMargin, 0))
        mbrankImageView.ff_AlignHorizontal(type: ff_AlignType.CenterRight, referView: nameLabel, size: CGSizeMake(14, 14), offset: CGPointMake(LRStatusCellMargin, 0))
        timeLabel.ff_AlignHorizontal(type: ff_AlignType.BottomRight, referView: iconView, size: nil, offset: CGPointMake(LRStatusCellMargin, 0))
        sourceLabel.ff_AlignHorizontal(type: ff_AlignType.CenterRight, referView: timeLabel, size: nil, offset: CGPointMake(LRStatusCellMargin, 0))
        verifiedImageView.ff_AlignInner(type: ff_AlignType.BottomRight, referView: iconView, size: CGSize(width: 17, height: 17), offset: CGPoint(x: 8.5, y: 8.5))
        contentLabel.ff_AlignVertical(type: ff_AlignType.BottomLeft, referView: timeLabel, size: nil, offset: CGPointMake(0, LRStatusCellMargin))
        //设置内容右边约束 不要忘记前面加contentView
        contentView.addConstraint(NSLayoutConstraint(item: contentLabel, attribute: NSLayoutAttribute.Right, relatedBy: NSLayoutRelation.Equal, toItem: contentView, attribute: NSLayoutAttribute.Right, multiplier: 1, constant: -LRStatusCellMargin))
        //设置contentView底部约束
        contentView.addConstraint(NSLayoutConstraint(item: contentView, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: contentLabel, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: LRStatusCellMargin))
    }

    // MARK: - 懒加载--------------------------------------
    /// 1 用户头像
    private lazy var iconView: UIImageView = {
        let imageView = UIImageView(image: UIImage(named: "avatar"))
        imageView.layer.cornerRadius = 17.5
        imageView.layer.masksToBounds = true
        return imageView
    }()
    
    /// 2 用户名称
    lazy var nameLabel: UILabel = UILabel(fontSize: 16, textColor: UIColor.darkGrayColor())
    
    /// 3 时间label
    lazy var timeLabel: UILabel = UILabel(fontSize: 10, textColor: UIColor.orangeColor())
    
    /// 4 来源label
    lazy var sourceLabel: UILabel = UILabel(fontSize: 10, textColor: UIColor.orangeColor())
    
    /// 5 认证图标
    lazy var verifiedImageView = UIImageView()
    
    /// 6 会员等级
    lazy var mbrankImageView: UIImageView = UIImageView(image: UIImage(named: "common_icon_membership"))
    
    /// 7 内容
    lazy var contentLabel: UILabel = {
        let label = UILabel(fontSize: 14, textColor: UIColor.darkGrayColor())
        label.numberOfLines = 0
        return label
    }()
}
